home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 13 / QRZ Ham Radio Callsign Database - Volume 13.iso / unix / src / clook.c < prev    next >
C/C++ Source or Header  |  1996-06-23  |  2KB  |  154 lines

  1.  
  2.  
  3.  
  4. #include "cb.h"
  5. #define SECTOR 512
  6.  
  7. clook(fp,call,rets)
  8. FILE *fp;
  9. char *call;
  10. char *rets;
  11. {
  12.  
  13.     long    pos;
  14.     long    disp;
  15.     char    str[256];
  16.     char    tmp[32];
  17.     char    kall[8];
  18.     char    buf[1024];
  19.     char    *p;
  20.     char    *d;
  21.     int    i;
  22.     int    n;
  23.     int    found;
  24.     int    seeks;
  25.     long    end;
  26.  
  27.     fseek(fp,0,SEEK_END);
  28.     end = ftell(fp);
  29.     pos = end/2;
  30.     disp = pos;
  31.  
  32.     p = kall;
  33.     while (*call)
  34.         *p++ = toupper(*call++);
  35.  
  36.     *p = '\0';
  37.  
  38.     seeks = 0;
  39.     found = 0;
  40.     while (!found)
  41.     {
  42.         memset(buf,0,sizeof(buf));
  43.         memset(str,0,sizeof(str));
  44.         memset(tmp,0,sizeof(tmp));
  45.  
  46.         seeks++;
  47.         fseek(fp,pos,SEEK_SET);
  48.         fread(buf,SECTOR,1,fp);
  49.         p = buf;
  50.  
  51.                 /* move to next whole entry */
  52.         while (*p && (*p != '\n'))
  53.             p++;
  54.  
  55.         p++;
  56.  
  57.         if (!*p)
  58.             break;    /* end of file */
  59.  
  60.         d = str;    /* get the whole entry */
  61.  
  62.         while (*p && (*p != '\n'))
  63.                 *d++ = *p++;
  64.  
  65.         d = tmp;
  66.         p = str;    /* get just the call */
  67.  
  68.         while (*p != DELIMITER)
  69.                 *d++ = *p++;
  70.  
  71. /*
  72.         n = callcmp(tmp,kall);
  73. */
  74.         n = strcmp(tmp,kall);
  75.  
  76.         if (n == 0)
  77.         {
  78.             strcpy(rets,str);
  79.             found++;
  80.             break;
  81.         }
  82.  
  83.         if (!disp)
  84.         {
  85.             break;
  86.         }
  87.  
  88.         disp = disp >> 1;
  89.  
  90.         if (n > 0)
  91.             pos -= disp;
  92.  
  93.         else
  94.             pos += disp;
  95.  
  96.     }
  97. /*
  98.     if (found)
  99.         printf("Found in %d seeks\n\n",seeks);
  100. */
  101.  
  102.     return found;
  103. }
  104.  
  105.  
  106.  
  107. callcmp(str,call)
  108. char *str;
  109. char *call;
  110. {
  111.     char s1[8];
  112.     char c1[8];
  113.     char *ps;
  114.     char *pc;
  115.     char *ds;
  116.     char *dc;
  117.     int n;
  118.             /* compare the AB#CDE sequence */
  119.  
  120.     strcpy(s1,str);
  121.     strcpy(c1,call);
  122.  
  123.     ps = s1;
  124.     ps++;
  125.     if (!isdigit(*ps))
  126.         ps++;        /* point to #CDE */
  127.     ds = ps;
  128.  
  129.     pc = c1;
  130.     pc++;
  131.     if (!isdigit(*pc))
  132.         pc++;        /* point to #CDE */
  133.     dc = pc;
  134.  
  135.     if (n = strcmp(ps,pc))
  136.         return(n);    /* unequal suffixes */
  137.  
  138.     /* suffixes are equal - check prefixes AB */
  139.  
  140.     *ds = '\0';
  141.     *dc = '\0';
  142.  
  143.     if ((int)strlen(s1) == (int)strlen(c1))
  144.         return strcmp(s1,c1);
  145.  
  146.                 /* AB vs A */
  147.     if (s1[1])
  148.         return 1;
  149.     else            /* A vs AB */
  150.         return -1;
  151.  
  152. }
  153.  
  154.